/* eslint valid-jsdoc: "off" */

'use strict';

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1644289387089_7321';

  // add your middleware config here, 注意要采用驼峰式的写法
  config.middleware = [ 'errorHandler', 'auth' ];

  // auth中间件的配置，除了注册、登录接口外，其他接口都需要使用该中间件
  config.auth = {
    match: [ '/logout', '/user/follow', '/user/unfollow',
      '/user/follows/', '/user/fans/', '/user/fan_drop',
      '/user/statistics', '/user/followed',
      '/user/list', '/user/:id/status', '/user/:id/password', '/user/:id/info',
      '/video', '/video/', '/video_read_auth/',
      '/video_detail', '/video_detail/', '/video_detail/destroy',
      '/fava', '/unfava',
      '/comment', '/video_comment_auth/:video_id',
      '/vod/sign', '/upload',
      '/admin/user_info', '/admin/logout', '/admin/list',
      '/admin/:id/enable', '/admin/:id/disable', '/admin/:id/destroy',
      '/admin/:id/reset_pwd', '/admin/:id/change_pwd',
      '/role', '/role/list', '/role/key', '/role/:id/', '/role/:id/enable', '/role/my',
      '/role/:id/disable', '/role/:id/destroy',
      '/department/tree', '/department/:id/', '/department/:id/move',
      '/advertisement/list', '/advertisement/:id/status', '/advertisement/:id', '/advertisement',
      '/plan_list', '/plan/:id/video', '/plan/:id/get', '/plan_week', '/plan_update',
      '/plan_students', '/plan/:id/advice',
      '/live/create', '/live_list/:page', '/live/:id/config', '/live/:id/status',
    ],
  };

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  config.security = {
    // 关闭 csrf
    csrf: {
      enable: false,
    },
    // 跨域白名单
    // domainWhiteList: ['http://localhost:7001'],
  };
  // 允许跨域的方法
  config.cors = {
    origin: '*',
    allowMethods: 'GET, PUT, POST, DELETE, PATCH',
  };

  // 数据库配置
  config.sequelize = {
    dialect: 'mysql',
    host: '127.0.0.1',
    username: 'video', // 正式: 'video'   本地：'root'
    password: 'GfpeB8PPB6cH3FHd', // 正式：'GfpeB8PPB6cH3FHd' 本地：'11111111'
    port: 3306,
    database: 'video', // 正式: 'video'   本地：'egg-video'
    // 中国时区
    timezone: '+08:00',
    define: {
      // 取消数据表名复数
      freezeTableName: true,
      // 自动写入时间戳 created_at updated_at
      timestamps: true,
      // 字段生成软删除时间戳 deleted_at
      // paranoid: true,
      createdAt: 'created_time',
      updatedAt: 'updated_time',
      // deletedAt: 'deleted_at',
      // 所有驼峰命名格式化
      underscored: true,
    },
  };

  // 参数验证
  config.valparams = {
    locale: 'zh-cn',
    // 是否出现错误后直接抛出error
    throwError: true,
  };

  // 加密
  config.crypto = {
    // 密钥可以自定义
    secret: '1qaz@4321_0987',
  };

  // jwt token
  config.jwt = {
    // 密钥可以自定义
    secret: '1qaz@4321_0987',
  };

  // redis
  config.redis = {
    client: {
      port: 6379, // Redis port
      host: '127.0.0.1', // Redis host
      password: '', // 密码 默认为空
      db: 0, // 选择第几个数据库
    },
  };

  config.multipart = {
    fileSize: '50mb',
    mode: 'stream',
    fileExtensions: [ '.xls', '.txt', '.jpg', '.JPG', '.png', '.PNG', '.gif', '.GIF', '.jpeg', '.JPEG' ],
  };

  // 改成你自己的！！！注意端口
  config.webUrl = '127.0.0.1:7001';

  config.tencentVod = {
    secret_id: 'AKIDb1ANJzNUKuDF0TkkSt4ybx3rOegRv83m',
    secret_key: 'UgbFcjZ8QRBernRoujHYYHjSu53j8H9k',
    vodSubAppId: 1300165652,
  };

  config.tencentCos = {
    secret_id: 'AKIDb1ANJzNUKuDF0TkkSt4ybx3rOegRv83m',
    secret_key: 'UgbFcjZ8QRBernRoujHYYHjSu53j8H9k',
    vodSubAppId: 1300165652,
    bucket: 'dark2000-1300165652',
    region: 'ap-shanghai',
  };

  config.view = {
    defaultViewEngine: 'nunjucks',
    mapping: {
      '.tpl': 'nunjucks',
    },
  };

  // 流媒体配置
  config.mediaServer = {
    rtmp: {
      port: 23480,
      chunk_size: 60000,
      gop_cache: true,
      ping: 30,
      ping_timeout: 60,
    },
    http: {
      port: 23481,
      allow_origin: '*',
    },
    auth: {
      play: true,
      publish: true,
      secret: 'nodemedia2017privatekey',
    },
  };

  // egg-socket.io
  config.io = {
    init: {
      wsEngine: 'ws',
    }, // passed to engine.io
    // namespace命名空间配置为/
    namespace: {
      '/': {
        // 预处理器中间件, 我们这里配置了一个auth, 进行权限判断, 它对应的文件是/app/io/middleware/auth.js, 这里可以配置多个文件, 用逗号隔开
        connectionMiddleware: [], // 这里我们可以做一些权限校验之类的操作
        packetMiddleware: [], // 通常用于对消息做预处理，又或者是对加密消息的解密等操作
      },
    },
    // 配置redis, 非必须, 不需要的可以不配置这块, egg-socket.io内置了socket-io-redis， 在cluster模式下, 使用redis可以较为简单的实现clients/rooms等信息共享
    redis: {
      host: '127.0.0.1',
      prot: 6379,
      auth_pass: '',
      db: 0,
    },
  };

  return {
    ...config,
    ...userConfig,
  };
};
